package org.chen.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.chen.domain.dto.AvatarExcludeDTO;
import org.chen.domain.dto.AvatarExcludeQueryDTO;
import org.chen.domain.dto.AvatarQueryDTO;
import org.chen.domain.dto.BatchDeleteExcludeDTO;
import org.chen.domain.entity.AvatarExclude;
import org.chen.domain.entity.VideoAvatar;
import org.chen.domain.response.PageResult;
import org.chen.domain.response.Result;
import org.chen.domain.vo.AvatarVO;
import org.chen.service.AvatarExcludeService;
import org.chen.service.AvatarService;
import org.chen.task.AvatarSyncTask;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/video/avatar")
@Slf4j
public class AvatarController {
    
    @Autowired
    private AvatarService avatarService;

    @Autowired
    private AvatarSyncTask avatarSyncTask;

    @Autowired
    private AvatarExcludeService avatarExcludeService;

    /**
     * 获取虚拟人物列表（支持分类查询）
     */
    @GetMapping("/list")
    public Result<PageResult<AvatarVO>> getAvatarList(AvatarQueryDTO avatarQueryDTO) {
        try {
            PageResult<AvatarVO> avatars = avatarService.getAvatarList(avatarQueryDTO);
            return Result.success(avatars);
        } catch (Exception e) {
            log.error("Get avatar list failed", e);
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 获取虚拟人物分类统计
     */
    @GetMapping("/stats")
    public Result<Map<String, Long>> getAvatarStats() {
        try {
            Map<String, Long> stats = avatarService.getAvatarStats();
            return Result.success(stats);
        } catch (Exception e) {
            log.error("Get avatar stats failed", e);
            return Result.error(e.getMessage());
        }
    }

    /**
     * 获取虚拟人物
     */
    @GetMapping("/{id}")
    public Result<VideoAvatar> getAvatarById(@PathVariable String id) {
        try {
            VideoAvatar avatar = avatarService.getAvatarById(id);
            return Result.success(avatar);
        } catch (Exception e) {
            log.error("Get avatar stats failed", e);
            return Result.error(e.getMessage());
        }
    }

    @PostMapping("/sync")
    public Result<Void> manualSync() {
        try {
            // 先删除所有现有角色
            avatarService.remove(new QueryWrapper<VideoAvatar>().ne("is_public", true));
            // 触发同步任务
            avatarSyncTask.syncAvatars();
            return Result.success();
        } catch (Exception e) {
            log.error("Manual sync failed", e);
            return Result.error("同步失败：" + e.getMessage());
        }
    }

    @GetMapping("/exclude/list")
    public Result<PageResult<AvatarExclude>> getExcludeList(AvatarExcludeQueryDTO avatarExcludeQueryDTO) {
        return Result.success(avatarExcludeService.getExcludeAvatar(avatarExcludeQueryDTO));
    }

    @PostMapping("/exclude")
    public Result<Void> addExclude(@RequestBody AvatarExcludeDTO exclude) {
        avatarExcludeService.saveAvatarName(exclude);
        return Result.success();
    }

    @DeleteMapping("/exclude/{id}")
    public Result<Void> removeExclude(@PathVariable Long id) {
        avatarExcludeService.removeById(id);
        return Result.success();
    }

    @DeleteMapping("/exclude/batch")
    public Result<Void> batchDelete(@RequestBody BatchDeleteExcludeDTO ids) {
        avatarExcludeService.batchDeleteByIds(ids);
        return Result.success();
    }
}